
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import javax.servlet.http.HttpServletRequest;

import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 验证api
 */
 
@Api(tags = "Validator",description = "验证")
@RestController
public class ValidatorController {
    
    @Autowired
    private ValidatorDao dao;
   
    @Autowired
    private ProjectDao daoProject;
    
    /**
     * 获取验证集合
     * @return api结果
     */
    @ApiOperation(value = "获取验证集合")
    @RequestMapping(value =  "/api/Validator/{phone}/{code}",method = {RequestMethod.GET})
    public ApiResult<List<Validator>> Get(@PathVariable String phone,@PathVariable String code){
         ApiResult<List<Validator>> result=new ApiResult<List<Validator>>();
        try 
        {
            Validator res = dao.findByPhoneAndCodeAndIsPassage(phone,code,0);

            if(res==null){
                result.setHasError(true);
                result.setMessage("验证码错误");
            }else if(res.getSignDate().getTime()<new Date().getTime()){
                result.setHasError(true);
                result.setMessage("验证码超时");
            }else{
                res.setIsPassage(1);

                dao.save(res);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取验证信息
     * @param id 验证ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取验证信息")
    @RequestMapping(value =  "/api/Validator/{id}",method = {RequestMethod.GET})
    public ApiResult<Validator> Get(@PathVariable Long id){
        ApiResult<Validator> result=new ApiResult<Validator>();
        try
        {
            Validator model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改验证信息
     * @param id 验证ID
     * @param model 验证信息
     * @return api结果
     */
    @ApiOperation(value = "修改验证信息")
    @RequestMapping(value =  "/api/Validator/{id}",method = {RequestMethod.PUT})
    public ApiResult<Validator> Edit(@PathVariable Long id,@RequestBody Validator model){
        ApiResult<Validator> result=new ApiResult<Validator>();
        try
        {
            model.setValidatorId(id);
            dao.save(model);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

 

    /**
     * 添加验证信息
     * @param model 验证信息
     * @return api结果Send
     */
    @ApiOperation(value = "添加验证短信信息")
    @RequestMapping(value =  "/api/Validator/",method = {RequestMethod.PATCH})
    public ApiResult<Validator> Send(@RequestBody Validator model,HttpServletRequest request){
         ApiResult<Validator> result=new ApiResult<Validator>();
        try
        {
            Optional<Project> project = daoProject.findById(model.getCode());

            if(project.isPresent()){
                
                String dt=String.valueOf(new Date().getTime()).substring(9);

                model.setCode(dt);

                HttpJsonUtil.send(model.getPhone(), "验证码:"+model.getCode()+"，您的预登记验证码，5分钟有效", project.get().getSignName());
                Date afterDate = new Date(new Date().getTime() + 300000);
                model.setCreateDate(new Date());
                model.setSignDate(afterDate);
                model.setIsPassage(0);
                model.setIp(request.getRemoteAddr());
                dao.save(model);
                result.setData(model);
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 添加验证信息
     * @param model 验证信息
     * @return api结果
     */
    @ApiOperation(value = "添加验证信息")
    @RequestMapping(value =  "/api/Validator/",method = {RequestMethod.POST})
    public ApiResult<Validator> Add(Validator model,HttpServletRequest request){
         ApiResult<Validator> result=new ApiResult<Validator>();
        try
        {
            Optional<Project> project = daoProject.findById(model.getCode());

            if(project.isPresent()){
                
                String dt=String.valueOf(new Date().getTime()).substring(9);

                model.setCode(dt);

                String data="{\"account\":\"dh81653\",\"password\":\""+
                TenpayUtil.MD5("e3YiG73c").toLowerCase()
                +"\",\"msgid\":\""
                +(UUID.randomUUID().toString())
                +"\",\"phones\":"
                +model.getPhone()
                +",\"content\":\""
                +"您的验证码为:"
                +model.getCode()
                +"\",\"sign\":\"【"
                +project.get().getSignName()
                +"】\",\"subcode\":\"\",\"sendtime\":\"\"}";

                TenpayHttpClient httpClient = new TenpayHttpClient(); 

                httpClient.callHttpPost("http://www.dh3t.com/json/sms/Submit", data);
                String  resContent = httpClient.getResContent();

                if(resContent.contains("提交成功")){ 
                    Date afterDate = new Date(new Date().getTime() + 300000);
                    model.setCreateDate(new Date());
                    model.setSignDate(afterDate);
                    model.setIsPassage(0);
                    model.setIp(request.getRemoteAddr());
                    dao.save(model);
                    result.setData(model);
                }else{
                    result.setHasError(true);
                    result.setMessage("发送短信失败");
                }
            }
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除验证信息
     * @param id 验证ID
     * @return api结果
     */
    @ApiOperation(value = "删除验证信息")
    @RequestMapping(value =  "/api/Validator/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

